package name.gauravmadan.mdg.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import name.gauravmadan.mdg.pim.Association;
import name.gauravmadan.mdg.pim.Class;
import name.gauravmadan.mdg.service.ModelDataService;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AssociationListController extends MultiActionController{
	private ModelDataService service;
	
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = null;
		String containerClassId = request.getParameter("containerClassId");
		if (containerClassId != null && !"".equals(containerClassId)) {
			List<Association> associationList = service.getAssociationListByClassId(new Integer(containerClassId));
			mav = new ModelAndView("association-list");
			mav.addObject("associationList", associationList);
			
			// To build the link for Class List, we need to fetch the component id corresponding to the parentClassId
			Class clazz = service.getClassById(new Integer(containerClassId));
			mav.addObject("parentComponentId",clazz.getParentComponentId());
		}
		else {
			// Return error here
		}
		return mav;
	}

	public ModelAndView showFormNew(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = null;
		Association association = new Association();
		String containerClassId = request.getParameter("containerClassId");
		if (containerClassId != null && !"".equals(containerClassId)){
			association.setContainerClassId(new Integer(containerClassId));
			mav = new ModelAndView("association-form");
			mav.addObject("association", association);
			
			// Add some reference data
			Integer parentComponentId = service.getClassById(new Integer(containerClassId)).getParentComponentId();
			List<Class> allClassList = service.getClassListByCompId(parentComponentId);
			mav.addObject("allClassList", allClassList);
		}
		else {
			// Return error here
		}
		return mav;
	}
	public ModelAndView showFormEdit(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedAssociationId = request.getParameter("selectedAssociationId");
		// Load Association Object based on selected association id
		ModelAndView mav = new ModelAndView("association-form");
		Association association = null;
		if (selectedAssociationId != null && !"".equals(selectedAssociationId)) {
			association = service.getAssociationById(new Integer(selectedAssociationId));
			
			// Add some reference data
			String containerClassId = request.getParameter("containerClassId");
			if (containerClassId != null && !"".equals(containerClassId)){
				Integer parentComponentId = service.getClassById(new Integer(containerClassId)).getParentComponentId();
				List<Class> allClassList = service.getClassListByCompId(parentComponentId);
				mav.addObject("allClassList", allClassList);
			}
		}
		else {
			// Return error here
		}
		mav.addObject("association", association);
		return mav;
	}
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedAssociationId = request.getParameter("selectedAssociationId");
		if (selectedAssociationId != null && !"".equals(selectedAssociationId)) {
			service.deleteAssociationById(new Integer(selectedAssociationId));
		}

		ModelAndView mav = new ModelAndView("redirect:/association-list.html?method=list");
		mav.addObject("containerClassId", request.getParameter("containerClassId"));
		return mav;
	}
	
	public ModelDataService getService() {
		return service;
	}
	public void setService(ModelDataService service) {
		this.service = service;
	}
}
